//
//  AIViewController.m
//  GoBangGraduationProject
//
//  Created by 刘人华 on 16/5/30.
//  Copyright © 2016年 dahua. All rights reserved.
//

#import "AIViewController.h"
#import "HBPlaySoundUtil.h"
#import "ChessBoardView.h"
#import "SFDraggableDialogView.h"

@interface AIViewController ()<SFDraggableDialogViewDelegate> {
    
    __weak IBOutlet UIImageView *backgroundImgView;
    ChessBoardView *boardView;
    __weak IBOutlet UIButton *soundBtn;
}
@property (nonatomic, strong) DHUserModel *user;
@property (nonatomic, strong) UILabel *labName;
@property (nonatomic, strong) UILabel *labLevel;
@property (nonatomic, strong) UILabel *labCount;
@end

@implementation AIViewController

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AIWhoWinNotify object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     通知
     */
    soundBtn.selected = ![DHNSUserdefault getTheIsSounds];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whoWin:) name:AIWhoWinNotify object:nil];
    boardView = [[ChessBoardView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_W, SCREEN_W)];
    backgroundImgView.userInteractionEnabled = YES;
    [backgroundImgView addSubview:boardView];
    
    [self setUserView];
    [self reloadUserData];
}
- (void)setUserView {
    CGFloat labWidth = SCREEN_W / 2.0f;
    CGFloat labHeight = 30;
    [backgroundImgView addSubview:self.labName];
    [self.labName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backgroundImgView.mas_top).offset(50 + SCREEN_W + 20);
        make.left.equalTo(backgroundImgView.mas_left);
        //make.center.equalTo(self.labName.superview);
        make.width.mas_equalTo(labWidth);
        make.height.mas_equalTo(labHeight);
    }];
    [backgroundImgView addSubview:self.labLevel];
    [self.labLevel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labName.mas_bottom).offset(10);
        make.left.equalTo(self.labName.mas_left);
        make.width.mas_equalTo(labWidth);
        make.height.mas_equalTo(labHeight);
    }];
    [backgroundImgView addSubview:self.labCount];
    [self.labCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labLevel.mas_bottom).offset(10);
        make.left.equalTo(self.labName.mas_left);
        make.width.mas_equalTo(labWidth);
        make.height.mas_equalTo(labHeight);
    }];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 9;
    bgView.clipsToBounds = YES;
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 16, 16)];
    imgView.image = [UIImage imageNamed:ColorNames[[DHNSUserdefault getTheUserColor]]];
    [bgView addSubview:imgView];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:bgView];
    self.navigationItem.rightBarButtonItem = rightBtn;
}
- (void)reloadUserData {
    self.labName.text = self.user.userName;
    self.labLevel.text = [NSString stringWithFormat:@"%@ %ld%@", self.user.userLevelDesc, (long)self.user.grade, ATLocalizedString(@"Points", @"分")];
    self.labCount.text = [NSString stringWithFormat:@"%@:%@ %lu %@ %lu", ATLocalizedString(@"Record", @"战绩"), ATLocalizedString(@"Total", @"总"), (unsigned long)self.user.totalCount, ATLocalizedString(@"Win", @"胜"), (unsigned long)self.user.winCount];
    
}
- (IBAction)startAgainClick:(UIButton *)sender {
    
    [[HBPlaySoundUtil shareForPlayingSoundEffectWith:@"start.wav"] play];
    [boardView reset];
}
- (IBAction)undoClick:(UIButton *)sender {
    
    [[HBPlaySoundUtil shareForPlayingSoundEffectWith:@"regret.wav"] play];
    [boardView undo];
}
- (IBAction)soundClick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    [DHNSUserdefault setupIsSounds:!sender.isSelected];
}

- (void)whoWin:(NSNotification *)notify {
    
    OccupyType type = [notify.userInfo[@"type"] integerValue];
    SFDraggableDialogView *v = [[SFDraggableDialogView alloc] init];
    if (type == OccupyTypeAI) {
        v = [SFDraggableDialogView drgViewWith:self titleText:ATLocalizedString(@"Game_Over", @"游戏结束了") photo:[UIImage imageNamed:@"lostPic.jpg"] message:ATLocalizedString(@"Game_Lose_To_AI", @"You lost to the AI")];
        v.delegate = self;
        self.user.grade -= 18;
        self.user.totalCount += 1;
    } else if (type == OccupyTypeUser) {
        v = [SFDraggableDialogView drgViewWith:self titleText:ATLocalizedString(@"Game_Over", @"游戏结束了") photo:[UIImage imageNamed:@"winPic.png"] message:ATLocalizedString(@"Game_Win_To_AI", @"Nice,you win!")];
        v.delegate = self;
        self.user.grade += 20;
        self.user.totalCount += 1;
        self.user.winCount += 1;
    }
    [ATLocal updateUser:_user];
    [self reloadUserData];
    
    [self.view addSubview:v];
}

- (void)draggableDialogView:(SFDraggableDialogView *)dialogView didPressFirstButton:(UIButton *)firstButton {
    
    [boardView reset];
    [dialogView dismissWithDrop:YES];
}

#pragma mark - setter
- (DHUserModel *)user {
    if (!_user) {
        _user = [ATLocal user];
    }
    return _user;
}
- (UILabel *)newLab {
    UILabel *lab = [[UILabel alloc] init];
    lab.textColor = App_Color;
    lab.textAlignment = NSTextAlignmentCenter;
    return lab;
}
- (UILabel *)labName {
    if (!_labName) {
        _labName = [self newLab];
    }
    return _labName;
}
- (UILabel *)labLevel {
    if (!_labLevel) {
        _labLevel = [self newLab];
    }
    return _labLevel;
}
- (UILabel *)labCount {
    if (!_labCount) {
        _labCount = [self newLab];
    }
    return _labCount;
}

@end
